package com.chuangke.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chuangke.admin.data.builder.UserDataBuilder;
import com.chuangke.common.constant.CacheKeyConstants;
import com.chuangke.common.data.builder.DataBuildConfig;
import com.chuangke.common.data.builder.DataBuilder;
import com.chuangke.common.db.page.CkPageQuery;
import com.chuangke.common.db.page.PageUtil;
import com.chuangke.common.idgen.IdManager;
import com.chuangke.framework.model.BasicModel;
import com.chuangke.system.dao.mapper.SysMeasureUnitMapper;
import com.chuangke.system.data.builder.MeasureGroupBuilder;
import com.chuangke.system.entity.SysMeasureUnit;
import com.chuangke.system.service.SysMeasureUnitService;
import org.springframework.aop.framework.AopContext;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SysMeasureUnitServiceImpl extends ServiceImpl<SysMeasureUnitMapper, SysMeasureUnit>
		implements SysMeasureUnitService {

	@Override
	public IPage<SysMeasureUnit> findPage(CkPageQuery page) {
		Map<String, SysMeasureUnit> map = ((SysMeasureUnitService) AopContext.currentProxy()).findAll();
		List<SysMeasureUnit> reportList = new ArrayList<>(map.values());

		// @ TODO 加入条件过滤
		String groupId = page.get("groupId") == null ? null : String.valueOf(page.get("groupId"));
		if (StrUtil.isNotBlank(groupId)) {
			reportList = reportList.stream().filter(unit -> groupId.equals(unit.getGroupId())).collect(Collectors.toList());
		}

		IPage<SysMeasureUnit> resultPage = PageUtil.findPage(page, reportList);

		DataBuilder
				.of(resultPage.getRecords()).appendBuilder(new UserDataBuilder(),
						new DataBuildConfig("createdBy", "createdBy"), new DataBuildConfig("updatedBy", "updatedBy"))
				.appendBuilder(new MeasureGroupBuilder(),
						new DataBuildConfig("groupId", "groupName"))
				.build();
		return resultPage;
	}

	@CacheEvict(cacheNames = CacheKeyConstants.SYS_MEASUREUNIT, key = "'" + CacheKeyConstants.SYS_MEASUREUNIT + "'")
	@Override
	public void saveOrUpdateData(SysMeasureUnit record) {
		if (StrUtil.isBlank(record.getId())) {
			record.setId(IdManager.nextId());
			this.save(record);
		} else {
			this.updateById(record);
		}
	}

	@CacheEvict(cacheNames = CacheKeyConstants.SYS_MEASUREUNIT, key = "'" + CacheKeyConstants.SYS_MEASUREUNIT + "'")
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void deleteDatas(List<String> ids) {
		this.removeByIds(ids);
	}

	@Cacheable(cacheNames = CacheKeyConstants.SYS_MEASUREUNIT, key = "'" + CacheKeyConstants.SYS_MEASUREUNIT + "'")
	@Override
	public Map<String, SysMeasureUnit> findAll() {
		List<SysMeasureUnit> list = this.list();
		return list == null ? new HashMap<>() : list.stream().collect(Collectors.toMap(BasicModel::getId, a -> a));
	}

	@Override
	public SysMeasureUnit find(String id) {
		Map<String, SysMeasureUnit> map = ((SysMeasureUnitService) AopContext.currentProxy()).findAll();
		return map.get(id);
	}

}
